package cn.ruituo.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebFilter(value = "/*",filterName = "bbb",displayName = "bbb")
public class RoleFiler implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入了验证过滤器");
        //session中有没有登录的信息
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        String path=request.getServletPath(); // 斜杠开头，上面加上妖访问的资源
        if(path.equals("/login.jsp")||path.equals("/doLogin.jsp")||path.endsWith(".js")||path.endsWith(".css")||path.endsWith(".png")){
            filterChain.doFilter(request,servletResponse);
            return;
        }
        Object o=request.getSession().getAttribute("USER_LOGIN");
        if(o==null){
            request.setAttribute("msg","您没有登录，无权访问");
            request.getRequestDispatcher("/login.jsp").forward(request,servletResponse);
            return;
        }
        filterChain.doFilter(request,servletResponse);

        System.out.println("离开了验证过滤器");
    }

    @Override
    public void destroy() {

    }
}
